package com.ambientrs.bot.io;

import java.io.*;
import java.net.URL;

public class IOManager {

    public static String home = new File(System.getProperty("user.home")
            + "/ambientrs/").toString();
    public static String cache = new File(home + "/cache/").toString();
    public static String log = new File(home + "/log/").toString();

    public static void cache(URL url, String name) throws IOException {
        File file = new File(cache + "/" + name);
        new File(cache).mkdirs();
        if (file.exists())
            file.delete();
        file.createNewFile();
        InputStream in = url.openStream();
        OutputStream out = new FileOutputStream(file);
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }

    public static File getCachedFile(String name) {
        return new File(cache + "/" + name);
    }

    public static void writeFile(String name, String text) throws IOException {
        BufferedWriter out = new BufferedWriter(new FileWriter(new File(home
                + "/" + name)));
        out.write(text);
        out.close();
    }

}
